/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Lee No Son
 */
public class MySQLConnect {
    String User = "";
    String Pass = "";
    String Database = "";
    String Host = "";
    
    ResultSet result = null;
    Statement statement = null;
    Connection connect = null;
    
    public MySQLConnect()
    {
        this.User = "root";
        this.Pass = "";
        this.Database = "qlkhoabenhvien";
        this.Host = "Localhost";
    }
    private Connection getConnection () throws Exception
    {
        String url = "jdbc:mysql://" + this.Host + ":3306/" + this.Database + "?useUnicode=true&characterEncoding=UTF-8";
        this.connect = DriverManager.getConnection(url, this.User, this.Pass);
        return this.connect;
    }
    
    private Statement getStatement () throws Exception
    {
        this.statement = getConnection().createStatement();
        return this.statement;
    }
    
    public ResultSet executeQuery (String query)throws Exception
    {
        this.result = getStatement().executeQuery(query);
        return this.result;
    }
    
    public int executeUpdate(String query)throws Exception
    {
        int d;
        d = getStatement().executeUpdate(query);
        return d;
    }
    
    public int CountRows (ResultSet r)throws Exception
    {
        int d = 0;
        while(r.next())
        {
            d++;
        }
        return d;
    }
}
